/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution162 {

    public static void main(String args[]) {
        Solution162 s = new Solution162();
        System.out.println(s.findPeakElement(new int[]{5, 10, 4, 3}));
    }

    public int findPeakElement(int[] nums) {
        int l = 0;
        int r = nums.length - 1;
        int mid;
        while (l <= r) {
            mid = l + (r - l) / 2;
            if (is(nums, mid)) {
                return mid;
            } else if (nums[mid] < nums[mid + 1]) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return -1;
    }

    public boolean is(int[] nums, int idx) {
        if (nums.length == 1) {
            return true;
        }
        if (idx == 0) {
            return nums[idx] > nums[idx + 1];
        }
        if (idx == nums.length - 1) {
            return nums[idx] > nums[idx - 1];
        }
        return nums[idx] > nums[idx + 1] && nums[idx] > nums[idx - 1];

    }

}
